﻿using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using MyTask.Application.Services;
using MyTask.Domain.Commands;
using MyTask.Domain.Models;
using MyTask.Domain.Queries;
using MyTask.Infrastructure.Context;
using Newtonsoft.Json.Linq;

namespace MyTask.WebApi.Controllers
{
    [Route("api/task")]
    [ApiController]
    [Authorize]
    public class TaskInfoController : ControllerBase
    {
        #region  Private variable
        private readonly IConfiguration _configuration;
        private readonly ITaskInfoService _taskInfoService;
        #endregion
        #region Constructor
        public TaskInfoController(IConfiguration configuration, ITaskInfoService taskInfoService)
        {
            _taskInfoService = taskInfoService;
            _configuration = configuration;
        }
        #endregion
        #region Public Api
        /// <summary>
        /// Add task
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [Route("add")]
        [HttpPost]
        public ActionResult AddTaskInfo(TaskInfoCommand command)
        {
            bool result = false;    
            if (ModelState.IsValid)
            {
               result = _taskInfoService.AddTaskInfo(command);
            }
            return Ok(result);
        }
        /// <summary>
        /// Get tasks by user
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Route("user")]
        [HttpPost]
        public ActionResult GetTaskInfosByUser(GetTaskInfosByUserQuery query)
        {
            var result = _taskInfoService.GetTaskInfosByUser(query);
            return Ok(result);
        }
        /// <summary>
        /// Get a task by taskid
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Route("taskId")]
        [HttpPost]
        public ActionResult GetTaskInfoById(GetTaskInfoByIdQuery query)
        {
            var result = _taskInfoService.GetTaskInfoById(query);
            return Ok(result);
        }
        /// <summary>
        /// Update task
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [Route("update")]
        [HttpPost]
        public ActionResult UpdateTaskInfo(TaskInfoCommand command)
        {
            bool result = false;
            if (ModelState.IsValid)
            {
                result = _taskInfoService.UpdateTaskInfo(command);
            }
            return Ok(result);
        }
        /// <summary>
        /// Get shared tasks by user
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [Route("shared")]
        [HttpPost]
        public ActionResult GetSharedTasksByUser(GetSharedTasksByUserQuery query)
        {
            var result = _taskInfoService.GetSharedTasksByUser(query);
            return Ok(result);
        }
        /// <summary>
        /// Sharing tasks
        /// </summary>
        /// <param name="commands"></param>
        /// <returns></returns>
        [Route("sharedtasks")]
        [HttpPost]
        public ActionResult SharedTasks(List<ShareTaskCommand> commands)
        {
            bool result = false;
            result = _taskInfoService.SharedTasks(commands);
           
            return Ok(result);
        } 
        #endregion
    }
}